#include "StdAfx.h"
#include "SystemFacade.h"

#include <fstream>

SystemFacade::SystemFacade()	
{	
}


SystemFacade::~SystemFacade(void)
{
}

int SystemFacade::getNestsCount()	
{
	return station->GetCheckoutsCount() + station->GetDistributorsCount();
}

int SystemFacade::getMachinesCount(int nestId)
{
	return 1;
}

int SystemFacade::isMachineFree(int nestId, int machineId)
{
	return !station->GetMachine(nestId)->HasTask();
}

std::vector<int> SystemFacade::getNestOutConnections(int nestId)
{
	std::vector<int> connections;

	
	if(nestId < station->GetDistributorsCount())
	{		
		for(int i = 0; i < station->GetCheckoutsCount(); i++)
		{
			connections.push_back(i + station->GetDistributorsCount());
		}		
	}		

	return connections;
}

bool SystemFacade::hasNestInConnections(int nestId)
{	
	return nestId >= station->GetDistributorsCount();
}

int SystemFacade::getBufferCapacity(int nestId)
{
	return station->GetMachine(nestId)->GetCapacity();
}

int SystemFacade::getBufferCount(int nestId)
{
	int count = station->GetMachine(nestId)->GetCount() - 1;
	return count < 0 ? 0 : count;
}

void SystemFacade::tick()
{
	station->Simulate();
}

void SystemFacade::loadNetwork(std::string config)
{
	std::vector<int> distributorsCap;
	std::vector<int> checkoutsCap;
	std::vector<int> curve;

	std::fstream f = std::fstream("config.txt");

	int distributorsCount = 0;
	int checkoutsCount = 0;
	int curvePointsCount = 0;

	/*
	int Config::SecPerTick = 60;
float Config::GasPrice = 4.5;
int Config::TankMean = 40;
int Config::TankSd = 15;
int Config::PaySecondsMean = 3 * 60;
int Config::PaySd = 1;
float Config::DistSpeed = 0.25;
*/


	f >> Config::SecPerTick;
	f >> Config::GasPrice;
	f >> Config::TankMean;
	f >> Config::TankSd;
	f >> Config::PaySecondsMean;
	f >> Config::PaySd;
	f >> Config::DistSpeed;




	f >> distributorsCount;

	for(int i = 0; i < distributorsCount; i++)
	{
		int tmp;
		f >> tmp;
		distributorsCap.push_back(tmp);
	}

	f >> checkoutsCount;

	for(int i = 0; i < checkoutsCount; i++)
	{
		int tmp;
		f >> tmp;
		checkoutsCap.push_back(tmp);
	}

	f >> curvePointsCount;

	for(int i = 0; i < curvePointsCount; i++)
	{
		int tmp;
		f >> tmp;
		curve.push_back(tmp);
	}

	
	/*
	distributorsCap.push_back(3);
	distributorsCap.push_back(3);
	distributorsCap.push_back(3);
	distributorsCap.push_back(3);

	checkoutsCap.push_back(10);
	checkoutsCap.push_back(10);

	curve.push_back(50);
	curve.push_back(60);
	curve.push_back(20);
	*/

	station = new GasStation(distributorsCap, checkoutsCap, curve);
}

std::string SystemFacade::readLog()
{
	std::string str;	
	return str;
}

int SystemFacade::getSeconds()
{
	return station->GetSeconds();
}

std::string SystemFacade::getTimeFormatted()
{
	char tmp[1024] = {0};
	int s = station->GetSeconds();
	
	sprintf(tmp, "%dd, %dh, %dm, %ds", 
		s / (24 * 60 * 60), 
		(s % (24 * 60 * 60)) / (60 * 60),
		(s % (60 * 60)) / 60,
		s % 60);

	return std::string(tmp);
}

float SystemFacade::getIncome()
{
	return station->GetIncome();
}

float SystemFacade::getIncome1h()
{
	return station->GetIncome1h();
}

float SystemFacade::getIncome12h()
{
	return station->GetIncome12h();
}

float SystemFacade::getIncome24h()
{
	return station->GetIncome24h();
}

int SystemFacade::getHappyCliens()
{
	return station->GetHappyClients();
}

int SystemFacade::getDiscardedClients()
{
	return station->GetDiscardedClients();
}

float SystemFacade::getLastCurveValue()
{
	return station->GetLastCurveValue();
}